﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DelonixRegiaHMSys.Core.Interfaces;
using DelonixRegiaHMSys.Core.Models;

namespace DelonixRegiaHMSys.Infrastructure.Data
{
    public class EfRoomReservationQuery : IRoomReservationQuery
    {
        //public IEnumerable<RoomReservation> GetAll()
        //{
        //    var context = new hmsDBEntities();
        //    return context.roomReservations.Select(rr => new RoomReservation()
        //    {
        //        ReservationID = rr.reservationID,
        //        GuestID = rr.guestID,
        //        UserID = rr.userID,
        //        NumOfAdults = rr.numOfAdults,
        //        NumOfChildren = (int)rr.numOfChildren,
        //        ArrivalDate = rr.arrivalDate,
        //        RoomType = rr.roomType,
        //        BedType = rr.bedType,
        //        SmokingOrNot = rr.smokingOrNot,
        //        AddRemarks = rr.addRemarks,
        //        ReservationStatus = rr.reservationStatus
        //    });

        //}

        public IEnumerable<RoomReservation> GetReservationbyGuestID(string guestID, DateTime dateTime)
        {
            var context = new hmsDBEntities();
            var query = from r in context.roomReservations
                        where r.guestID == guestID && r.arrivalDate == dateTime
                        select new RoomReservation()
                        {
                            ReservationID = r.reservationID,
                            NumOfAdults = r.numOfAdults,
                            NumOfChildren = r.numOfChildren,
                            ArrivalDate = r.arrivalDate,
                            RoomType = r.preferredRoomType,
                            BedType = r.preferredBedType,
                            SmokingOrNot = r.smokingOrNot,
                            AddRemarks = r.addRemarks,
                            ReservationStatus = r.reservationStatus
                        };
            return query;
        }

        public void AddRoomReservation(RoomReservation rr)
        {
            var context = new hmsDBEntities();
            var newRoomReservation = context.roomReservations.Create();
            newRoomReservation.reservationID = rr.ReservationID;
            newRoomReservation.guestID = rr.GuestID;
            newRoomReservation.userID = rr.UserID;
            newRoomReservation.numOfAdults = rr.NumOfAdults;
            newRoomReservation.numOfChildren = rr.NumOfChildren;
            newRoomReservation.arrivalDate = rr.ArrivalDate;
            newRoomReservation.preferredRoomType = rr.RoomType;
            newRoomReservation.preferredBedType = rr.BedType;
            newRoomReservation.smokingOrNot = rr.SmokingOrNot;
            newRoomReservation.addRemarks = rr.AddRemarks;
            newRoomReservation.reservationStatus = rr.ReservationStatus;
            context.roomReservations.Add(newRoomReservation);
            context.SaveChanges();
        }

        public void UpdateRoomReservation(RoomReservation rr)
        {
            var context = new hmsDBEntities();
            var newRoomReservation = context.roomReservations.FirstOrDefault(r => r.reservationID == rr.ReservationID);
            newRoomReservation.numOfAdults = rr.NumOfAdults;
        }

        public void DeleteRoomReservation(string reservationID)
        {
        }

        public bool ReservationRecordAdded(string reservationID)
        {
            var context = new hmsDBEntities();
            var SuccessOrFail = context.roomReservations.FirstOrDefault(r => r.reservationID == reservationID);
            if (SuccessOrFail != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool RoomReservationTodayExist(string guestID)
        {
            var context = new hmsDBEntities();
            string a = DateTime.Now.Date.ToString("M/d/yyyy"); ;
            DateTime date = Convert.ToDateTime(a);
            var ExistOrNot = context.roomReservations.FirstOrDefault(r => r.guestID == guestID && r.arrivalDate == date);
            if (ExistOrNot != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
